<?php
class Z1ttsvsController extends AppController {

	var $name = 'Z1ttsvs';
    var $uses = array('Z1ttsv','Khoahoc','Chidoansv');

	function index($chidoan_id=null) {
		$this->Z1ttsv->recursive = 0;
        $tongiaos = $this->Z1ttsv->Tongiao->find('list',array('fields'=>'Tongiao.tentongiao'));
		$this->set('z1ttsvs', $this->paginate(array('chidoansv_id'=>$chidoan_id,'ngaychuyen'=>NULL)));
        $chidoan=$this->Z1ttsv->query('select tenchidoan from chidoansvs where id='.$chidoan_id);
        $this->set(compact('chidoan_id','chidoan','tongiaos'));
	}
    
    function search() {
        $this->layout='search';
        $column = array(
                        'Z1ttsv.mssv',
                        'Z1ttsv.hoten',
                        'Z1ttsv.phai',
                        'Z1ttsv.tongiao_id',
                        'Z1ttsv.tentinhtp',
                        'Z1ttsv.tenquanhuyen',
                        'Z1ttsv.tenphuongxa',
                       );
        $value = $this->params['pass'];
        //$ii = 7;
        for($i=0;$i<count($column);$i++){
            if($value[$i]!="null"){
                $conditions[] = array($column[$i].' Like'=>'%'.$value[$i].'%');
            }
        }
        //pr($conditions);
        if(!empty($conditions)){
            $results=$this->Z1ttsv->find('all',array('conditions'=>$conditions));
            $this->set('results', $results);    
        }//else $this->set('results', $this->paginate(array('chidoansv_id'=>$chidoan_id,'ngaychuyen'=>NULL)));
        $this->set('results', $results);          
    }
    
    function khoahoc($khoahoc_id=0,$chidoan_id=0){
        $khoahoc=$this->Khoahoc->find('list',array('field'=>'id'));
        $chidoan=$this->Chidoansv->find('list',array('conditions'=>array('khoahoc_id'=>$khoahoc_id,'khoa_id'=>$this->Session->read('khoa_id')),
                                                    'fields'=>array('tenchidoan')));
        $this->set(compact('khoahoc','khoahoc_id','chidoan_id','chidoan'));
    }
    
    function chidoan($chidoan_id=0){
        $khoahoc_id =$this->data['Z1ttsv']['khoahoc'];
        $chidoan=$this->Chidoansv->find('list',array('conditions'=>array('khoahoc_id'=>$khoahoc_id,'khoa_id'=>$this->Session->read('khoa_id')),
                                                    'fields'=>array('tenchidoan')));
        $this->set(compact('chidoan','chidoan_id'));
    }

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid z1ttsv', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('z1ttsv', $this->Z1ttsv->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Z1ttsv->create();
			if ($this->Z1ttsv->save($this->data)) {
				$this->Session->setFlash(__('The z1ttsv has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The z1ttsv could not be saved. Please, try again.', true));
			}
		}
		$tongiaos = $this->Z1ttsv->Tongiao->find('list');
		$phuongxas = $this->Z1ttsv->Phuongxa->find('list');
		$dantocs = $this->Z1ttsv->Dantoc->find('list');
		$chidoansvs = $this->Z1ttsv->Chidoansv->find('list');
		$this->set(compact('tongiaos', 'phuongxas', 'dantocs', 'chidoansvs'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid z1ttsv', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Z1ttsv->save($this->data)) {
				$this->Session->setFlash(__('The z1ttsv has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The z1ttsv could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Z1ttsv->read(null, $id);
		}
		$tongiaos = $this->Z1ttsv->Tongiao->find('list');
		$phuongxas = $this->Z1ttsv->Phuongxa->find('list');
		$dantocs = $this->Z1ttsv->Dantoc->find('list');
		$chidoansvs = $this->Z1ttsv->Chidoansv->find('list');
		$this->set(compact('tongiaos', 'phuongxas', 'dantocs', 'chidoansvs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for z1ttsv', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Z1ttsv->delete($id)) {
			$this->Session->setFlash(__('Z1ttsv deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Z1ttsv was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
